package gui.pannelPartie.MatchList;

import gui.BeginDialogue.ChargementException;
import gui.Error.ErrorDialogue;
import gui.Error.TextToInt;
import gui.Error.TextToIntException;
import html.HtmlBody;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import core.Exemp;
import core.Match;
import core.NotEndMatchException;
import core.Team;

public class MatchInfo extends JPanel implements ActionListener , HtmlBody{

	private Match match;
	private JLabel l1 ;
	private JLabel l2 ;
	private JTextField tf1;
	private JTextField tf2;
	private CheckBox cb1;
	private CheckBox cb2;
	private Boolean isScoreSubmit=false;
	private Frame onError;
	private static String FieldSaveSeparator=":";
	private static int EMPTY= -5000;
	
	public MatchInfo(Match m , Frame owner) {
		onError = owner;
		match=m;
		cb1=  new CheckBox(m.getTeam1Name(),this);
		cb2=  new CheckBox(m.getTeam2Name(),this);
		tf1 = new JTextField(4);
		tf2 = new JTextField(4);
		l1 = new Label(m.getTeam1Name());
		l2 = new Label(m.getTeam2Name());
		add(cb1);
		add(l1);
		add(tf1);
		add(tf2);
		add(l2);
		add(cb2);
	}

	public MatchInfo(Frame owner, String charge, Team[] teams,int inNbPoints) throws ChargementException {
		Scanner sc = new Scanner(charge);
		sc.useDelimiter(FieldSaveSeparator);
		onError = owner;
		cb1=  new CheckBox("",this);
		cb2=  new CheckBox("",this);
		tf1 = new JTextField(4);
		tf2 = new JTextField(4);
		l1 = new Label("");
		l2 = new Label("");
		add(cb1);
		add(l1);
		add(tf1);
		add(tf2);
		add(l2);
		add(cb2);
		if(sc.hasNextBoolean()){
			setVisible(sc.nextBoolean());
			if(isVisible()){
				Team t1,t2;
				if(sc.hasNextInt())
					t1=getTeam(sc.nextInt(),teams);
				else throw new ChargementException("Erreur de chargement");
				if(sc.hasNextInt())
					t2=getTeam(sc.nextInt(),teams);
				else throw new ChargementException("Erreur de chargement");
				match=new Match(t1,t2,inNbPoints);
				if(sc.hasNextBoolean())
					cb1.setSelected(sc.nextBoolean());
				else throw new ChargementException("Erreur de chargement");
				if(sc.hasNextBoolean())
					cb2.setSelected(sc.nextBoolean());
				else throw new ChargementException("Erreur de chargement");				
				int aux;
				if(sc.hasNextInt()){
					aux=sc.nextInt();
					tf1.setText((aux==EMPTY)?(""):(""+aux));	
				}
				else throw new ChargementException("Erreur de chargement");
				if(sc.hasNextInt()){
					aux=sc.nextInt();
					tf2.setText((aux==EMPTY)?(""):(""+aux));	
				}
				else throw new ChargementException("Erreur de chargement");
				if(cb1.isSelected()&&cb2.isSelected()){
					cb1.setEnabled(false);
					cb2.setEnabled(false);
					tf1.setEditable(false);
					tf2.setEditable(false);
					isScoreSubmit=true;
				}
				l1.setText(match.getTeam1Name());
				l2.setText(match.getTeam2Name());
				if(match.getTeam1Name().equals(Exemp.EXEMP.toString()))
					cb1.setEnabled(false);
				if(match.getTeam2Name().equals(Exemp.EXEMP.toString()))
					cb2.setEnabled(false);
			}
		}
		else throw new ChargementException("Erreur de chargement");
		sc.close();
	}

	private Team getTeam(int id, Team[] teams) throws ChargementException {
		Team res=null;
		if(id==Exemp.EXEMP.getId())
			res=Exemp.EXEMP;
		for(int i=0;(i<teams.length)&&(res==null);i++)
			if(id==teams[i].getId())
				res=teams[i];
		if(res==null)
			throw new ChargementException("Erreur de chargement");
		return res;
	}

	private static final long serialVersionUID = 1L;

	public void refresh(Match m){
		match=m;
		cb1.refresh(m.getTeam1Name());
		cb2.refresh(m.getTeam2Name());
		tf1.setEditable(true);
		tf2.setEditable(true);
		tf1.setText("");
		tf2.setText("");
		l1.setText(m.getTeam1Name());
		l2.setText(m.getTeam2Name());
		isScoreSubmit=false;
	}
	
	public void actionPerformed(ActionEvent e) {
		if(cb1.isSelected()&&cb2.isSelected()){
			try {
				submitScore();
			} catch (Exception e1) {
				cb1.refresh(match.getTeam1Name());
				cb2.refresh(match.getTeam2Name());
				new ErrorDialogue( onError,e1.getMessage());
			}
		}
	}

	public void submitScore() throws NotEndMatchException, TextToIntException {

			if(!isScoreSubmit)
			   match.setScore(TextToInt.getValueOf(tf1.getText()), TextToInt.getValueOf(tf2.getText()));
			isScoreSubmit=true;
			tf1.setEditable(false);
			tf2.setEditable(false);
			cb1.setEnabled(false);
			cb2.setEnabled(false);
	}

	public String getHtmlBody() {
		StringBuffer res= new StringBuffer();
		res.append("<tr><td>"+l1.getText()+"</td>");
		res.append("<td>"+tf1.getText()+"</td>");
		res.append("<td>"+tf2.getText()+"</td>");
		res.append("<td>"+l2.getText()+"</td></tr>\n");
		return res.toString();
	}

	public String save() {
		StringBuffer b = new StringBuffer();
		b.append(isVisible());
		if(isVisible()){
			b.append(FieldSaveSeparator+match.getTeam1Id()+FieldSaveSeparator+match.getTeam2Id()+FieldSaveSeparator);
			b.append(cb1.isSelected()+FieldSaveSeparator);
			b.append(cb2.isSelected()+FieldSaveSeparator);
			try {
				b.append(TextToInt.getValueOf(tf1.getText())+FieldSaveSeparator);
			} catch (TextToIntException e) {
				b.append(EMPTY+FieldSaveSeparator);
			}
			try {
				b.append(TextToInt.getValueOf(tf2.getText())+FieldSaveSeparator);
			} catch (TextToIntException e) {
				b.append(EMPTY);
			}			
		}
		return b.toString();
	}
	

}
